﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wanet.PublicPortal.Common.Domain;
using NHibernate;
using log4net;
using Wanet.PublicPortal.Common.Utilities;

namespace Wanet.PublicPortal.Common.Managers
{
	public class RoleManager: BaseManager, IManager<Role>
	{
		private static readonly object _instanceLocker = new object();
		private static RoleManager _instance = null;

		/// <summary>
		/// Initializes a new instance of the <see cref="RoleManager"/> class.
		/// </summary>
		internal RoleManager()
		{
		}

		/// <summary>
		/// Gets the manager instance. This a singleton implementation. Only one instace of the manager will be created.
		/// </summary>
		/// <returns></returns>
		public static RoleManager GetManager()
		{
			if (_instance == null)
			{
				lock (_instanceLocker)
				{
					if (_instance == null)
					{
						_instance = new RoleManager();
					}
				}
			}

			return _instance;
		}

		#region IManager<Role> Members

		/// <summary>
		/// Creates the specified item. After creation it reads just persisted item and return it back as a result.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <returns></returns>
		public Role Create(Role item)
		{
			ITransaction _tran = null;
			using (_tran = Session.BeginTransaction())
			{
				try
				{
					_tran.Begin();
					Session.SaveOrUpdate(item);
					_tran.Commit();
				}
				catch (Exception ex)
				{
					Log.Error("Role could not be created. See the error message for more information.", ex);

					throw;
				}
			}

			return item;
		}

		/// <summary>
		/// Gets the item by specified ID.
		/// </summary>
		/// <param name="id">The ID.</param>
		/// <returns></returns>
		public Role Get(int id)
		{
			try
			{
				IQuery query = Session.CreateQuery("from Role where id=:id");
				query.SetInt32("id", id);

				return query.UniqueResult<Role>();
			}
			catch (Exception ex)
			{
				Log.Error("Getting role has failed. See the error message for more information.", ex);
				Log.ErrorFormat("Role ID: {0}", id);
				throw;
			}
		}

		/// <summary>
		/// Gets the list of all items.
		/// </summary>
		/// <returns></returns>
		public IList<Role> GetAll()
		{
			IQuery query = Session.CreateQuery("from Role");

			return query.List<Role>();
		}

		/// <summary>
		/// Updates the specified item. After update it returns back updated item as a result.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <returns></returns>
		public Role Update(Role item)
		{
			ITransaction _tran = null;
			using (_tran = Session.BeginTransaction())
			{
				try
				{
					_tran.Begin();
					Session.SaveOrUpdate(item);
					_tran.Commit();
				}
				catch (Exception ex)
				{
					Log.Error("Role could not be updated. See the error message for more information.", ex);

					throw;
				}
			}
			return item;
		}

		/// <summary>
		/// Deletes the specified item. It returns true if the specified item is removed correctly; otherwise false.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <returns></returns>
		public bool Delete(Role item)
		{
			ITransaction _tran = null;
			using (_tran = Session.BeginTransaction())
			{
				try
				{
					_tran.Begin();
					Session.Delete(item);
					_tran.Commit();

					return _tran.WasCommitted;
				}
				catch (Exception ex)
				{
					Log.Error("Role could not be deleted. See the error message for more information.", ex);

					throw;
				}
			}
		}

		#endregion
	}
}
